/**
 * Created by lixiangcheng on 2017/6/15.
 */

/**login
 *
 */
function register() {
    window.location.href = "../driver/sign/sign.html"
}
function forgotPassword(sourceFrom) {
    //sourceFrom:10-客户，20-司机
    window.location.href = "../login_forgot/forgot.html?sourceFrom=" + sourceFrom
}
//登陆
function loginForm() {
    var logonNam = $('#driverLoginForm').find("#logonNam").val();
    var password = $('#driverLoginForm').find("#password").val();
    if (logonNam == null || logonNam == '') {
        alert('用户名不能为空');
        return false;
    }
    if (password == null || password == '') {
        alert('密码不能为空');
        return false;
    }
    var url = _global.serverHost + "/driver/loginValide.htm";
    var data = $('#driverLoginForm').serialize();
    _callAjaxRequest(url, data, loginBack);
}
//登陆后操作
function loginBack(data) {
    if (data.outResult == "1") {
        setCookie("driverUuid", data.driverUuid);
        window.location.href = "../driver/index.html";
    } else {
        alert(data.outResultReason);
    }
}
///////


function setCookie(name, value) {
    localStorage.setItem(name, value);
}
function getCookie(name) {
    var nameValue = localStorage.getItem(name);
    if (nameValue == undefined) {
        nameValue = null;
        window.location.href = "../driver/login.html";
    }
    return nameValue;
}

function delCookie(name) {
    localStorage.removeItem(name);
}


function checkDriverToken(){
    var driverUuid=getCookie("driverUuid");
    if(driverUuid==undefined || driverUuid==null || driverUuid==''){
        window.location.href='../'+_global.driverLoginUrl;
    }else{
        $.ajax({
            type: 'POST',
            url: _global.serverHost + '/driver/validToken.htm',
            dataType: "json",
            data: { 'driverUuid': driverUuid},
            success: function (data) {
                if (data.outResult == '1') {
                    return '1';
                } else {
                    window.location.href='../'+_global.driverLoginUrl;
                }
            }
        });
    }
}

function checkDriverTokenIndex(){
    var driverUuid=getCookie("driverUuid");
    if(driverUuid==undefined || driverUuid==null || driverUuid==''){
        window.location.href=_global.driverLoginUrl;
    }else{
        $.ajax({
            type: 'POST',
            url: _global.serverHost + '/driver/validToken.htm',
            dataType: "json",
            data: { 'driverUuid': driverUuid},
            success: function (data) {
                if (data.outResult == '1') {
                	localStorage.setItem("driverInfo", JSON.stringify(data));//保存司机信息
                    return '1';
                } else {
                    window.location.href= _global.driverLoginUrl;
                }
            }
        });
    }
}